package com.youlai.boot.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.system.converter.SpotTravelConverter;
import com.youlai.boot.system.enums.YesNoEnum;
import com.youlai.boot.system.mapper.SpotTravelMapper;
import com.youlai.boot.system.model.entity.SpotTravel;
import com.youlai.boot.system.model.form.SpotTravelForm;
import com.youlai.boot.system.model.query.SpotTravelQuery;
import com.youlai.boot.system.model.vo.SpotTravelVO;
import com.youlai.boot.system.service.SpotTravelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 旅行社数据服务实现类
 *
 * @author lwb
 * @since 2025-06-29 21:14
 */
@Service
@RequiredArgsConstructor
public class SpotTravelServiceImpl extends ServiceImpl<SpotTravelMapper, SpotTravel> implements SpotTravelService {

    private final SpotTravelConverter spotTravelConverter;

    /**
     * 获取旅行社数据分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<SpotTravelVO>} 旅行社数据分页列表
     */
    @Override
    public IPage<SpotTravelVO> getSpotTravelPage(SpotTravelQuery queryParams) {
        Page<SpotTravelVO> pageVO = this.baseMapper.getSpotTravelPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取旅行社数据表单数据
     *
     * @param id 旅行社数据ID
     * @return
     */
    @Override
    public SpotTravelForm getSpotTravelFormData(Long id) {
        return this.baseMapper.getSpotTravelFormData(id);
    }

    /**
     * 新增旅行社数据
     *
     * @param formData 旅行社数据表单对象
     * @return
     */
    @Override
    public boolean saveSpotTravel(SpotTravelForm formData) {
        SpotTravel entity = spotTravelConverter.toEntity(formData);
        fieldFill(entity);
        return this.save(entity);
    }

    /**
     * 更新旅行社数据
     *
     * @param id       旅行社数据ID
     * @param formData 旅行社数据表单对象
     * @return
     */
    @Override
    public boolean updateSpotTravel(Long id, SpotTravelForm formData) {
        SpotTravel entity = spotTravelConverter.toEntity(formData);
        fieldFill(entity);
        return this.updateById(entity);
    }

    public void fieldFill(SpotTravel entity) {
        Long userId = SecurityUtils.getUserId();
        LocalDateTime now = LocalDateTime.now();
        if (entity.getId() == null) {
            entity.setCreateBy(userId);
            entity.setCreateTime(now);
            entity.setUpdateBy(userId);
            entity.setUpdateTime(now);
        } else {
            entity.setUpdateBy(userId);
            entity.setUpdateTime(now);
        }
    }

    /**
     * 删除旅行社数据
     *
     * @param ids 旅行社数据ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSpotTravels(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的旅行社数据数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public SpotTravel getSpotTravel(Long deptId, Long categoryId) {
        return baseMapper.getSpotTravel(deptId, categoryId);
    }

    /**
     * @param idList
     * @return
     */
    @Override
    public List<SpotTravel> getByCategorysId(List<Long> idList) {
        LambdaQueryWrapper<SpotTravel> lqw = new LambdaQueryWrapper<>();
        lqw.in(SpotTravel::getCategoryId, idList);
        lqw.gt(SpotTravel::getNum, YesNoEnum.NO.getType());
        return this.list(lqw);
    }

    /**
     * @param idList
     * @return
     */
    @Override
    public List<SpotTravel> getByDeptIds(List<Long> idList) {
        LambdaQueryWrapper<SpotTravel> lqw = new LambdaQueryWrapper<>();
        lqw.in(SpotTravel::getDeptId, idList);
        lqw.gt(SpotTravel::getNum, YesNoEnum.NO.getType());
        return this.list(lqw);
    }
}
